
//  HMLoginView.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class HMLoginView: UIView {
    
    func updateUI(isHome: Bool,title: String? = nil,imageName: String? = nil){
        if isHome {
            startAnimation()
        }else{
            bringSubviewToFront(imageView)
            circleIcon.hidden = true
        }
        if title != nil {
             textLabel.text = title!
        }
        if imageName != nil {
             imageView.image = UIImage(named: imageName!)
        }
        
       
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(red: 237.0/255.0, green: 237.0/255.0, blue: 237.0/255.0, alpha: 1)
        addUI()
    }
    private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 15
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        circleIcon.layer.addAnimation(animation, forKey: nil)
    }
    private func addUI(){
        self.addSubview(circleIcon)
        self.addSubview(textLabel)
        self.addSubview(loginBtn)
        self.addSubview(registerBtn)
        self.addSubview(coverView)
        self.addSubview(imageView)
    }
    override func layoutSubviews() {
        //1 苹果原生的NSConstraints
        circleIcon.translatesAutoresizingMaskIntoConstraints = false;
       self.addConstraint(NSLayoutConstraint(item:circleIcon , attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        self.addConstraint(NSLayoutConstraint(item:circleIcon , attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -100))
        //VFL语句
        /*
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[circleIcon]-(16)-[textLabel]", options: [], metrics: nil, views: ["circleIcon":circleIcon,"textLabel":textLabel]))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[textLabel(230)]-|", options: .AlignAllCenterX, metrics: nil, views: ["textLabel":textLabel]))
 */
        textLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(circleIcon.snp_bottom).offset(30)
            make.width.equalTo(230)
            make.height.equalTo(50)
        }
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        imageView.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon)
            
        }
        coverView.snp_makeConstraints { (make) in
            make.center.equalTo(circleIcon)
        }
        
    }
    //懒加载实现控件
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy var textLabel: UILabel = {
        let label:UILabel = UILabel()
        label.text = "瞧一瞧,看一看,赶紧注册赢大奖!"
        label.font = UIFont.systemFontOfSize(17)
        label.textAlignment = .Center
        
        label.numberOfLines = 0
        return label
    }()
    var loginBtn: UIButton = {
        
        let btn = UIButton()
        let image = UIImage(named: "common_button_white_disable")
        let w = (image?.size.width)! * 0.5
        let h = (image?.size.width)! * 0.5
        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(Int(w), topCapHeight: Int(h)), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        return btn
        
    }()
    var registerBtn: UIButton = {
        
        let btn = UIButton()
        let image = UIImage(named: "common_button_white_disable")
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(image, forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return btn
        
    }()
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "visitordiscover_feed_image_house")
        imageView.sizeToFit()
        return imageView
    }()
    private lazy var coverView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        imageView.sizeToFit()
        return imageView
    }()
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
